package com.huiketong.cofpasgers.controller;

import com.huiketong.cofpasgers.constant.Constant;
import com.huiketong.cofpasgers.json.LayuiUploadResp;
import com.huiketong.cofpasgers.json.data.UpLoadFileData;
import com.huiketong.cofpasgers.json.response.BaseJsonResponse;
import com.huiketong.cofpasgers.util.DateUtils;
import com.huiketong.cofpasgers.util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller
public class FileUploadController {
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    public BaseJsonResponse upload(@RequestParam("file") MultipartFile file) throws FileNotFoundException, ParseException {
        BaseJsonResponse resObj = new BaseJsonResponse();

        String saveDirectory = FileUtil.getUploadDir();
        return FileUtil.upLoad(file, resObj, saveDirectory);
    }

    @PostMapping("layuiupload")
    @ResponseBody
    @CrossOrigin
    public LayuiUploadResp layuiupload(@RequestParam("file") MultipartFile file) throws ParseException {
        LayuiUploadResp resp = new LayuiUploadResp();

        if (file != null && !file.isEmpty()) {
            //获取上传的文件名
            String tempfile = file.getOriginalFilename();
            String suffix = tempfile.substring(tempfile.lastIndexOf(".") + 1);
            String fileName = DateUtils.dateFormat(new Date(), DateUtils.LINE_DATETIME_PATERN);
            String whole_name = fileName + "." + suffix;
            try {
                FileUtil.uploadFile(file.getBytes(), Constant.UPLOAD_PATH, whole_name);
                resp.setCode(0);
                resp.setMsg("");
                LayuiUploadResp.DataBean dataBean = new LayuiUploadResp.DataBean();
                dataBean.setSrc(Constant.IMAGE_URL + whole_name);
                resp.setData(dataBean);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            resp.setCode(200);
            resp.setMsg("文件不存在");
            resp.setData(null);
        }

        return resp;
    }


    @RequestMapping(value = "/upload/batch", method = RequestMethod.POST)
    @CrossOrigin
    @ResponseBody
    public BaseJsonResponse batchUpload(HttpServletRequest request) throws ParseException {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        MultipartFile file;
        BaseJsonResponse resObj = new BaseJsonResponse();
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            String saveDirectory = "/images";
            return FileUtil.upLoad(file, resObj, saveDirectory);
        }
        return resObj;
    }


}
